Skip to content

metro-file-map: Remove FallbackWatcher's 1s startup delay on Windows - #2106

Merged
robhogan merged 1 commit into
pr2105from
pr2106
Nov 17, 2026
Merged

robhogan merged 1 commit into
pr2105from
pr2106

Conversation

@robhogan

@robhogan robhogan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

On Windows, FallbackWatcher waits a second after setting up its watches:

if (platform === 'win32') {
// Inherited from sane: win32 needs a settling period after the watches
// are registered before events can be relied on.
await new Promise(resolve => setTimeout(resolve, 1000));
}

That's a holdover from sane. libuv issues ReadDirectoryChangesW before uv_fs_event_start returns, so a watch is live as soon as fs.watch returns (here in libuv 1.49.2, which Node 22.13 ships):

https://github.com/libuv/libuv/blob/e1095c7a4373ce00cd8874d8e820de5afb25776e/src/win/fs-event.c#L298-L320

Benchmark

Unsurprisingly, this takes ~exactly one second off watch-mode startup on Windows (1,941ms -> 935ms), and is a no-op elsewhere.

main #2105 This PR vs main vs #2105
Linux: Watch-mode start, warm cache 1,887ms 975ms 969ms -48.7% (-49.2% to -47.9%) -0.7% (-1.4% to +1.2%)
Windows: Watch-mode start, warm cache 2,577ms 1,941ms 935ms -63.7% (-63.9% to -63.6%) -51.8% (-52.0% to -51.7%)
Linux: Watch-mode start, cold cache 1,885ms 921ms 931ms -50.6% (-51.1% to -49.8%) +1.1% (-0.2% to +2.6%)
Windows: Watch-mode start, cold cache 2,562ms 1,918ms 917ms -64.2% (-64.4% to -64.0%) -52.2% (-52.5% to -52.1%)

Changelog

 - **[Performance]**: metro-file-map: Remove a 1s delay from file watcher startup on Windows without Watchman

Test plan

New unit test that changes made as soon as startWatching resolves are reported. It runs on Linux and Windows, and is skipped on macOS, where fs.watch starts FSEvents asynchronously (but FallbackWatcher isn't used). Benchmark as in #2104.

@robhogan
robhogan added this pull request to stack #2109 October 25, 2026 15:22
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 25, 2026
@robhogan
robhogan force-pushed the pr2106 branch 2 times, most recently from 115807f to b1b6854 Compare November 10, 2026 09:13
robhogan added a commit that referenced this pull request Nov 10, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 27-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,933ms to 505ms (-74%) on Linux, and from 2,584ms to 547ms (-79%) on Windows. RSS after startup is 6MB (4%) higher on Linux, though the heap is unchanged. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 6% of #2104, which is noise: #2106 changes nothing there and measures +5.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,933ms | 716ms | 505ms | -73.9% (-74.0% to -73.6%) | -29.5% (-29.8% to -28.8%) |
| Windows: Watch-mode start, warm cache | 2,584ms | 745ms | 547ms | -78.8% (-78.9% to -78.7%) | -26.6% (-26.9% to -26.3%) |
| Linux: Watch-mode start, cold cache | 1,923ms | 706ms | 503ms | -73.8% (-74.0% to -73.4%) | -28.7% (-29.2% to -27.6%) |
| Windows: Watch-mode start, cold cache | 2,569ms | 733ms | 532ms | -79.3% (-79.4% to -79.2%) | -27.4% (-27.7% to -27.0%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.2% (-11.2% to -11.2%) | -0.0% (-0.0% to -0.0%) |
| Linux: RSS after a watch-mode start | 188.0MB | 160.9MB | 166.7MB | -11.3% (-11.7% to -10.5%) | +3.6% (+3.2% to +3.9%) |
| Windows: RSS after a watch-mode start | 209.5MB | 185.4MB | 183.8MB | -12.2% (-12.7% to -10.7%) | -0.8% (-1.0% to -0.6%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan marked this pull request as ready for review November 10, 2026 16:17
@robhogan
robhogan requested a review from huntie November 10, 2026 16:18
robhogan added a commit that referenced this pull request Nov 11, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
@robhogan
robhogan requested a review from vzaidman November 12, 2026 17:27
On Windows, `FallbackWatcher` waits a second after setting up its watches:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L77-L82

That's a holdover from `sane`. libuv issues `ReadDirectoryChangesW` before `uv_fs_event_start` returns, so a watch is live as soon as `fs.watch` returns (here in libuv 1.49.2, which Node 22.13 ships):

https://github.com/libuv/libuv/blob/e1095c7a4373ce00cd8874d8e820de5afb25776e/src/win/fs-event.c#L298-L320

## Benchmark

Unsurprisingly, this takes ~exactly one second off watch-mode startup on Windows (1,941ms -> 935ms), and is a no-op elsewhere.

|  | `main` | #2105 | This PR | vs `main` | vs #2105 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 975ms | 969ms | -48.7% (-49.2% to -47.9%) | -0.7% (-1.4% to +1.2%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 1,941ms | 935ms | -63.7% (-63.9% to -63.6%) | -51.8% (-52.0% to -51.7%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 921ms | 931ms | -50.6% (-51.1% to -49.8%) | +1.1% (-0.2% to +2.6%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 1,918ms | 917ms | -64.2% (-64.4% to -64.0%) | -52.2% (-52.5% to -52.1%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Remove a 1s delay from file watcher startup on Windows without Watchman
```

## Test plan

New unit test that changes made as soon as `startWatching` resolves are reported. It runs on Linux and Windows, and is skipped on macOS, where `fs.watch` starts FSEvents asynchronously (but `FallbackWatcher` isn't used). Benchmark as in #2104.
@robhogan
robhogan removed this pull request from stack #2109 November 16, 2026 22:09
@robhogan
robhogan added this pull request to stack #2241 November 16, 2026 22:10
@robhogan
robhogan requested review from huntie and removed request for huntie November 17, 2026 18:41
@robhogan
robhogan merged commit d032fa5 into main Nov 17, 2026
16 checks passed
robhogan added a commit that referenced this pull request Nov 17, 2026
#2107)

`FallbackWatcher` keeps an entry for every file it has seen:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L53-L56

It only reads them when something is deleted. Deleting `src/a.js` is reported because there's an entry for it, and deleting `src` reports `src/a.js`, `src/b.js` and so on from the entries, then `src` itself.

Neither needs an entry per file:

 - A file's name says whether it was reported: `src/a.js` passes the include checks, `src/README.md` doesn't.
 - Told that `src` is deleted, the file map removes everything under it (that's how `NativeWatcher` reports it), so one `delete` is enough.

So this keeps entries only for directories (including ones it couldn't watch) and symlinks. Those are reported whatever their name, so once `src/components` is gone, only an entry says whether it was a directory or a file the include checks left out.

## Benchmark

This takes another 22-30% off watch-mode startup on Linux and Windows (Linux: 969ms -> 695ms), and 3MB (10%) off the heap after startup on this app.

|  | `main` | #2106 | This PR | vs `main` | vs #2106 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 969ms | 695ms | -63.2% (-63.6% to -62.8%) | -28.2% (-29.2% to -27.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 935ms | 727ms | -71.8% (-71.9% to -71.7%) | -22.2% (-22.4% to -21.9%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 931ms | 652ms | -65.4% (-65.6% to -65.1%) | -30.0% (-30.9% to -29.2%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 917ms | 701ms | -72.7% (-72.8% to -72.5%) | -23.6% (-23.8% to -23.1%) |
| Linux: Heap after a watch-mode start | 31.7MB | 31.2MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -9.9% (-9.9% to -9.9%) |
| Windows: Heap after a watch-mode start | 31.7MB | 31.3MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -9.9% (-9.9% to -9.9%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Start the file watcher faster, and with less memory, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` that drives FileMap in watch mode over a real directory, deleting directories and symlinks and moving directories out (with `FallbackWatcher`, or `NativeWatcher` on macOS). Benchmark as in #2104.
robhogan added a commit that referenced this pull request Nov 17, 2026
Without Watchman, `Watcher` starts its backends before the crawl, so that nothing changed during the crawl is missed:

https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/Watcher.js#L118-L127

For `FallbackWatcher` that means walking the tree once to watch it and again to crawl it. This lets the crawl do both: the crawler hands each directory to the watcher just before listing it, the order the watcher's own walk uses. Directories the crawl skips as ignored (eg `node_modules` without `retainAllFiles`) are handed over too, for the watcher to walk unless it ignores them as well.

This is internal to the built-in node crawler. A `crawlerFactory` crawler, a recrawl, a root added with `addRoot` and the node crawl after a failed Watchman crawl keep the watcher's own walk.

## Benchmark

This takes another 28-30% off watch-mode startup on Linux and Windows. With the rest of this stack, watch-mode startup without Watchman goes from 1,887ms to 492ms (-74%) on Linux, and from 2,577ms to 525ms (-80%) on Windows. On macOS, which uses `NativeWatcher`, PRs 2-5 stay within 5% of #2104, which is noise: #2106 changes nothing there and measures +4.6%.

|  | `main` | #2107 | This PR | vs `main` | vs #2107 |
|---|---|---|---|---|---|
| Linux: Watch-mode start, warm cache | 1,887ms | 695ms | 492ms | -73.9% (-74.2% to -73.7%) | -29.2% (-29.9% to -28.6%) |
| Windows: Watch-mode start, warm cache | 2,577ms | 727ms | 525ms | -79.6% (-79.7% to -79.5%) | -27.7% (-28.1% to -27.4%) |
| Linux: Watch-mode start, cold cache | 1,885ms | 652ms | 462ms | -75.5% (-75.8% to -75.0%) | -29.2% (-30.0% to -27.7%) |
| Windows: Watch-mode start, cold cache | 2,562ms | 701ms | 493ms | -80.8% (-80.8% to -80.6%) | -29.6% (-29.9% to -29.3%) |
| Linux: Heap after a watch-mode start | 31.7MB | 28.1MB | 28.1MB | -11.3% (-11.3% to -11.3%) | -0.1% (-0.1% to -0.1%) |
| Windows: Heap after a watch-mode start | 31.7MB | 28.2MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -0.0% (-0.0% to -0.0%) |

## Changelog

```
 - **[Performance]**: metro-file-map: Set up file watching during the crawl instead of in a separate walk before it, on Linux and Windows without Watchman
```

## Test plan

New unit tests, and a `watch-integration-test.js` case for a directory the crawl skips but the watcher doesn't. Benchmark as in #2104.
robhogan added a commit to robhogan/expo that referenced this pull request Nov 19, 2026
# Why

Expo's `FallbackWatcher`, which Metro uses on Linux and Windows while we default Watchman off, is upstream's from March, plus twins of two later upstream PRs. Upstream has since fixed several bugs in it, and made it start faster with less memory:

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before listing it | expo#49363 |
| react/metro#1967 | Recrawl a directory when Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` | expo#51093 |
| react/metro#2004 | Close every watch under a deleted directory | - |
| react/metro#2105 | Start without a stat per file | - |
| react/metro#2106 | Remove the 1s startup delay on Windows | - |
| react/metro#2107 | Track only directories and symlinks | - |

The fixes matter most on Windows. A deleted watched directory reports its own deletion to its handle again and again until the handle is closed (before libuv 1.53), and the open handle stops the directory being watched again if it's recreated. When changes overflow a directory's buffer, Windows doesn't name what changed, and Expo's watcher reported one file it guessed had changed and missed the rest.

Elsewhere, a handle on Linux follows a directory that's moved aside (as npm does to update a package), so it went on watching the old copy. And `walker` doesn't descend into a watch root that's a symlink, so nothing below one was watched.

The faster startup also offsets the cost of reading symlinks at crawl since expo#53732, on Linux and Windows.

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's `FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It leaves out `isLiveOnStart()` from react/metro#2003, which only upstream's `Watcher` calls, and react/metro#2108, which needs `Watcher` to start its backends before the crawl (react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher that errors does nothing, since Node closes the handle itself before emitting `error`.

A directory replaced at the same path, a symlink replaced by a directory and a change Windows doesn't name now have the file map recrawl the path, which `FileMap` already handles. For the last, a gap remains until we port react/metro#2104: a recrawl doesn't stat a file the file map never has, so a change to one is still missed, as all but one were before.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two tests for expo#48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms -> 338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node 22.13.0 and 24, and on Windows 11 with Node 24, where it's the default watcher.
- Checked that a watch root that's a symlink is now watched below the root.

Benchmark (AI-driven), against `main`: starting the watcher on a default Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default source extensions, 16 interleaved rounds per platform, each run in a fresh process, with a byte-identical control whose medians are all within 0.5% of `main`. Heap and RSS are the growth while watching, after a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |
robhogan added a commit to expo/expo that referenced this pull request Nov 20, 2026
# Why

`FallbackWatcher` is used as Metro's file watcher in Linux and Windows
while we default Watchman off. Upstream has since fixed several bugs in
it, and made it start faster with less memory (offsetting eager
symlinks: #53732):

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list
directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before
listing it | #49363 |
| react/metro#1967 | Recrawl a directory when
Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` |
#51093 |
| react/metro#2004 | Close every watch under a
deleted directory | - |
| react/metro#2105 | Start without a stat per
file | - |
| react/metro#2106 | Remove the 1s startup delay
on Windows | - |
| react/metro#2107 | Track only directories and
symlinks | - |

The most significant fixes are on Windows:
- A deleted watched directory reports its own deletion to its handle
again and again until the handle is closed
(libuv/libuv#5013,
nodejs/node#61398), and the open handle stops
the directory being watched again if it's recreated.
- When changes overflow a directory's buffer, Windows doesn't name what
changed. The fork guessed at one file and missed the rest - there's now
a `'recrawl'` event to catch up.
 
On Linux, probably the most significant is we now correctly watch
directory moves in/out of watches, typical in package installs (also
using `recrawl`).

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's
`FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It
leaves out `isLiveOnStart()` from
react/metro#2003, which only upstream's
`Watcher` calls, and react/metro#2108, which
needs `Watcher` to start its backends before the crawl
(react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher
that errors does nothing, since Node closes the handle itself before
emitting `error`.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two
tests for #48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms ->
338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which
was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node
22.13.0 and 24, and on Windows 11 with Node 24, where it's the default
watcher.
- Checked that a watch root that's a symlink is now watched below the
root.

Benchmark (AI-driven), against `main`: starting the watcher on a default
Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default
source extensions, 16 interleaved rounds per platform, each run in a
fresh process, with a byte-identical control whose medians are all
within 0.5% of `main`. Heap and RSS are the growth while watching, after
a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |
robhogan added a commit to expo/expo that referenced this pull request Nov 24, 2026
# Why

`FallbackWatcher` is used as Metro's file watcher in Linux and Windows
while we default Watchman off. Upstream has since fixed several bugs in
it, and made it start faster with less memory (offsetting eager
symlinks: #53732):

| Upstream | What | Expo |
|---|---|---|
| react/metro#1906 | Drop `walker`, list
directories from a bounded pool | - |
| react/metro#1907 | Watch a directory before
listing it | #49363 |
| react/metro#1967 | Recrawl a directory when
Windows doesn't name a change | - |
| react/metro#2000 | Remove `micromatch` |
#51093 |
| react/metro#2004 | Close every watch under a
deleted directory | - |
| react/metro#2105 | Start without a stat per
file | - |
| react/metro#2106 | Remove the 1s startup delay
on Windows | - |
| react/metro#2107 | Track only directories and
symlinks | - |

The most significant fixes are on Windows:
- A deleted watched directory reports its own deletion to its handle
again and again until the handle is closed
(libuv/libuv#5013,
nodejs/node#61398), and the open handle stops
the directory being watched again if it's recreated.
- When changes overflow a directory's buffer, Windows doesn't name what
changed. The fork guessed at one file and missed the rest - there's now
a `'recrawl'` event to catch up.

On Linux, probably the most significant is we now correctly watch
directory moves in/out of watches, typical in package installs (also
using `recrawl`).

# How

This replaces `FallbackWatcher.ts` with a TypeScript port of upstream's
`FallbackWatcher.js` at react/metro@82d957f, and drops `walker`. It
leaves out `isLiveOnStart()` from
react/metro#2003, which only upstream's
`Watcher` calls, and react/metro#2108, which
needs `Watcher` to start its backends before the crawl
(react/metro#2054) first.

The old file had nothing upstream lacks. Its extra handling of a watcher
that errors does nothing, since Node closes the handle itself before
emitting `error`.

The tests are upstream's `FallbackWatcher-test.js`, plus the fork's two
tests for #48950.

## Performance

On a default Expo app, the watcher starts 2.6x faster on Linux (872ms ->
338ms) and 3.8x faster on Windows (1859ms -> 487ms, a second of which
was the delay), with 53-66% less RSS.

# Test Plan

- Unit tests (786).
- The `FallbackWatcher` tests three times each on Linux with Node
22.13.0 and 24, and on Windows 11 with Node 24, where it's the default
watcher.
- Checked that a watch root that's a symlink is now watched below the
root.

Benchmark (AI-driven), against `main`: starting the watcher on a default
Expo SDK 58 app (6,013 directories, 34,274 files) with Metro's default
source extensions, 16 interleaved rounds per platform, each run in a
fresh process, with a byte-identical control whose medians are all
within 0.5% of `main`. Heap and RSS are the growth while watching, after
a GC. Linux Mint 22.3 on an i5-4278U, and Windows 11.

| Platform | Metric | `main` | This PR [95% CI] |
|---|---|---|---|
| Linux | Startup | 871.6ms | -61.2% [-61.5, -60.8] |
| Linux | Heap | 7.1MB | -30.1% [-30.1, -30.0] |
| Linux | RSS | 89.2MB | -65.8% [-66.5, -65.5] |
| Windows | Startup | 1859.3ms | -73.8% [-73.9, -73.6] |
| Windows | Heap | 7.1MB | -30.0% [-30.0, -30.0] |
| Windows | RSS | 116.4MB | -52.7% [-52.7, -52.4] |

(cherry picked from commit 5ed1f80)
robhogan added a commit that referenced this pull request Nov 26, 2026
Publish Metro 0.88.1 from `main`. Nothing since 0.88.0 is breaking, so this is a patch release, but one new default is worth reading about before upgrading: transform results are now shared between graphs (see below).

Changelog: Internal

## Draft release notes

```
 - **[Feature]**: metro-cache: `HttpStore` accepts `maxSockets`, to change its connection limit, and `getParams`, to add query parameters (such as a short-lived credential) to every request (#2246 by @GijsWeterings)
 - **[Feature]**: `Transforming file` end log entries report the worker's CPU time for the transform as `cpu_time_us` (#2282 by @vzaidman)
 - **[Fix]**: Keep function bindings that are reassigned when folding constants in production builds (#2197 by @Mwrnk)
 - **[Fix]**: Keep side effects such as `i++` in the leading operands of a sequence when folding constants in production builds (#2278 by @burrows99)
 - **[Fix]**: Keep a `var` declared in a branch that's removed when folding constants in production builds, if code outside the branch uses it, instead of throwing a `ReferenceError` (#2279 by @burrows99)
 - **[Fix]**: Don't replace `void 0` with `undefined` when folding constants in production builds, where `undefined` is shadowed (#2215 by @ZainnQureshii)
 - **[Fix]**: Memoized inline requires: A module first referenced in a skipped optional chain, logical assignment or default value is no longer `undefined` at later references (#2280 by @burrows99)
 - **[Fix]**: `experimentalImportSupport`: `export * from` a module with an enumerable `__esModule` no longer throws in strict mode (#2281 by @burrows99)
 - **[Fix]**: Define a `__proto__` key in JSON modules as an own property, matching `JSON.parse` (#2199 by @Mwrnk)
 - **[Fix]**: `FileStore` writes cache entries atomically, so concurrent reads no longer miss valid entries or see partial writes (#2040 by @frankleng)
 - **[Fix]**: Respect `excludeSource` in inline source maps, and pass it to `serializer.customSerializer` (#2256 by @robhogan)
 - **[Fix]**: Stop leaking a graph's `DeltaCalculator` and file change listener when its first build fails (#2283 by @sensei-woo)
 - **[Fix]**: Resolve a package subpath ending in `/.`, such as `pkg/.`, instead of throwing `Unexpectedly escaped traversal` (#2213 by @kwy404)
 - **[Fix]**: Fix `resolver.blockList: []` hiding every file (8f49d3681cfd)
 - **[Fix]**: Fix an out-of-memory crash on a symlink into itself (#2204 by @robhogan)
 - **[Fix]**: Fix the path of a file added through a symlink to the project root (#2212 by @robhogan)
 - **[Fix]**: Fix `Failed to collapse` on a path through a symlink out of the project root, into a directory whose name ends in `.` (#2237 by @robhogan)
 - **[Fix]**: `require.context` on a directory above the project root follows symlinks inside the project root, and lists each level correctly from two or more levels up (#2229 by @robhogan)
 - **[Fix]**: Use a separate file map cache under Bun, which can't read Node's cache (#2209 by @robhogan)
 - **[Fix]**: Include dependencies required by Metro's published TypeScript declarations (#2149 by @sorinc03)
 - **[Performance]**: Dev server memory is ~95% lower per lazy-loaded route (`import()` or async routes), and lazy bundle requests are ~2x faster (#2239 by @robhogan)
 - **[Performance]**: Without Watchman, the dev server is ready to bundle ~2.5-3x faster on Linux and Windows, and ~15% faster on macOS (#2104, #2105, #2106, #2107, #2108, #2219, #2220, #2221, #2240 by @robhogan)
 - **[Performance]**: Without Watchman, CI builds start bundling ~25-35% sooner on Linux and Windows, and ~15% sooner on macOS (#2104, #2219, #2220, #2240 by @robhogan)
 - **[Performance]**: At least 10x faster `composeSourceMaps` for large bundles (#2250 by @yhpark)
 - **[Types]**: Remove undeclared type-only imports between Metro packages. `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro` - annotate `Logger.on` handlers that read it as `(entry: MetroLogEntry) => ...` (#2257 by @robhogan)

> NOTE: Experimental features are not covered by semver and can change at any time.

 - **[Experimental]**: Share module outputs between graphs with equal transform options, on by default. Set `transformer.unstable_shareTransformResults: false` to opt out if a transform's output depends on inputs outside its cache key, or if a custom serializer writes to module outputs or dependencies (#2239, #2244 by @robhogan)
 - **[Experimental]**: Add `unstable_hedgeCacheReadsAfterMs`, which transforms a module locally when its cache read is slow and takes whichever finishes first, and `maxInFlightReads` on `HttpGetStore`, which misses rather than queueing once that many reads are outstanding (#2249 by @GijsWeterings)

 **Full Changelog**: v0.88.0...v0.88.1
```

## Shared transform results

`transformer.unstable_shareTransformResults` is on by default. `Transformer` keeps one in-memory copy of each module's `output` and `dependencies` per set of transform options, checked before `cacheStores`, and every graph with equal transform options gets that same object. That's where the lazy bundling memory savings come from. Set it to `false` for the previous behavior. It's left out of the transform cache key, so switching it doesn't invalidate any cache.

Two kinds of code depend on the old behavior and should opt out:

 - A transform whose output depends on inputs outside its cache key, and which relies on a cache store declining its result so that it runs again, now gets its first result back for as long as the key is unchanged. In Expo, this showed up as stale Tailwind CSS in dev.
 - Code that writes to `Module.output` or to dependency data (`TransformResultDependency`), which the types already make read-only, now changes every graph holding the same module. Custom serializers that edit outputs in place are the likely case.

Entries are released when no graph in an `IncrementalBundler` (ie `Server`, `runBuild`) holds the module any more. Code that constructs `Transformer` or `DeltaBundler` directly, without `IncrementalBundler`'s `onModulesReleased` wiring, never releases them, so a long-lived `Transformer` keeps the latest result for every module and transform options it has seen. Set the option to `false` there unless that's intended.

## Migration notes

None of these need a change for apps on Metro's public API, but integrations using `metro*/private/*` or Metro's types may need one:

 - `createFileMap` (`metro/private/node-haste/DependencyGraph/createFileMap`) returns `hasteMap: null` unless `hasteImplModulePath` is set or `enableGlobalPackages` is on, and its type is now `?HasteMap`. Callers that read it need a null check, eg `hasteMap?.getModule(...)`. Configs that set either option get the same `HastePlugin` and cache as before.
 - `CacheManager.write`'s `getSnapshot` now returns the file map's live tree, not a copy. A custom `unstable_fileMapCacheManagerFactory` cache manager must serialize what `getSnapshot` returns before any `await`, or copy it. `TreeFS.getSerializableSnapshot()` still returns a copy.
 - The node crawler no longer stats regular files up front, so `linkStats()` can return a null `modifiedTime` and `size` for a file Metro hasn't read. `CACHE_BREAKER` is bumped (14 -> 15), so file map caches written by 0.88.0 are rebuilt once. Watchman crawls are unaffected.
 - File map plugin names must be unique across all plugins. A plugin's `onChanged` may return a summary, published by plugin name on `ChangeEvent.pluginChanges`, and code that builds `ChangeEvent`s by hand needs a `pluginChanges` map.
 - `TreeFS` gives up on a path after 40 symlink follows, as Linux does, and reports it missing.
 - The file map's `FileSystem` interface has `isMapped`, and a missing `LookupResult` carries `canonicalMissingPath` and `remainingPath`, so a custom `FileSystem` implementation needs the method and code that builds missing results by hand needs the fields. A `TreeFS` constructed without `setRoots` treats every path as mapped, as before.
 - Type-only, from removing undeclared type imports between packages: `bundle_options` moves from `metro-core`'s `LogEntry` to `MetroLogEntry`, exported by `metro`, so annotate `Logger.on('log', (entry: MetroLogEntry) => ...)` handlers that read it. `AmbiguousModuleResolutionError#hasteError` is a structural interface, no longer assignable to `DuplicateHasteCandidatesError`. Custom resolvers can't read other fields of `context.dependency.data` (use `context.isESMImport`). `MinifierResult.map` and `decodedMap` are read-only.
 - `excludeSource` is on `SerializerOptions`, typed `?boolean`, so a custom serializer's options type that narrows it to `boolean` no longer accepts Metro's.

## Test plan

```
node ./scripts/updateVersion 0.88.1
sl diff --stat
```

Only the 17 `packages/*/package.json` manifests change, and every `0.88.0` reference is replaced. Flow, ESLint, Prettier, TypeScript, API snapshots, Markdown lint and Jest pass on this commit.

Benchmark (AI-driven): the `[Performance]` figures are net against 0.88.0 from npm, with a byte-identical copy of 0.88.0 as a noise control. Startup used #2104's benchmark app on macOS, Linux and Windows, over 12 interleaved rounds, timed from process start to the first transform: a dev server (`runServer`, watching) taking an immediate bundle request, and a CI build (`runBuild` with `CI=1` and no file map cache). Lazy chunks used Mattermost's iOS dev server with 20 lazily loaded screens, and `composeSourceMaps` used Mattermost's Android release bundle (7,194 modules) and its `hermesc -O` map. Bundles and composed maps are byte-identical between the two versions. These were measured at 1f9812e. None of the seven later commits changes what they time: startup ends before the first transform, the lazy chunk runs read a warm cache, and #2287 changes the crawler only by an equivalent path join.

Expo `main` was validated against this release at 1f9812e58d8f, before the last seven commits on `main`, packed as 0.88.1 and wrapped as a local `@expo/metro` 58.2.0. Typecheck and unit tests pass across Expo's Metro packages, as do the repo build, React Server Components, native and Tailwind exports, and dev server SSR, bundles, source maps and file watching. Expo needed changes of its own (sharing off, for the Tailwind case above, and catching up with `excludeSource`, `reservedString` and `globalPrefix`), and no Metro changes. Of the seven later commits, Expo's defaults only reach the constant folding fixes #2215 and #2279, and only where `undefined` is shadowed or a dead branch's `var` is used outside it. Expo CLI has its own file map, so #2287 doesn't reach it, and Expo compiles imports and exports with its own plugin, doesn't memoize inline requires or hedge cache reads, and its default cache store writes atomically already.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants